package ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.service.imp;

import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.devicecontroller.DeviceManager;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.devicecontroller.DeviceState;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.devicecontroller.command.Command;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.devicecontroller.command.RegisterCommand;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.persistence.model.CompoundCommand;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.service.CompoundCommandService;

public class CompoundCommandServiceImp implements CompoundCommandService {

	private RegisterCommand cmd;
	private CompoundCommand cmpcmd;
	private DeviceManager devman;
	
	@Override
	public String getName() {
		return cmpcmd.getName();
	}

	@Override
	public synchronized void set(Integer value) {
		if(cmd.getState() == Command.WAITING || cmd.getState() == Command.PROCESSING )
			return;
		if(devman.getState() != DeviceState.CONNECTED){
			cmd.setState(Command.FAIL);
			return;
		}
		cmd.setValue(value);
		devman.execute(cmd);

	}

	@Override
	public int getState() {
		return cmd.getState();
	}

	/**
	 * @return the cmd
	 */
	public RegisterCommand getCmd() {
		return cmd;
	}

	/**
	 * @param cmd the cmd to set
	 */
	public void setCmd(RegisterCommand cmd) {
		this.cmd = cmd;
	}

	/**
	 * @return the cmpcmd
	 */
	public CompoundCommand getCmpcmd() {
		return cmpcmd;
	}

	/**
	 * @param cmpcmd the cmpcmd to set
	 */
	public void setCmpcmd(CompoundCommand cmpcmd) {
		this.cmpcmd = cmpcmd;
	}

	/**
	 * @return the devman
	 */
	public DeviceManager getDevman() {
		return devman;
	}

	/**
	 * @param devman the devman to set
	 */
	public void setDevman(DeviceManager devman) {
		this.devman = devman;
	}


}
